<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-header">
        <div class="logo">医</div>
        <h1 class="title">中医科普平台</h1>
        <p class="subtitle">传承千年智慧，守护健康人生</p>
      </div>
      
      <el-form 
        ref="formRef" 
        :model="form" 
        :rules="rules" 
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            size="large"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            size="large"
            show-password
            clearable
            @keyup.enter="handleSubmit"
          />
        </el-form-item>
        
        <el-form-item class="login-options" v-if="isLogin">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          <el-button link type="primary">忘记密码？</el-button>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            size="large"
            :loading="loading"
            @click="handleSubmit"
          >
            {{ isLogin ? '登录' : '注册' }}
          </el-button>
        </el-form-item>
        
        <div class="register-link">
          {{ isLogin ? '还没有账号？' : '已有账号？' }}
          <el-button 
            link 
            type="primary" 
            @click="switchMode"
          >
            {{ isLogin ? '立即注册' : '去登录' }}
          </el-button>
        </div>
      </el-form>
    </div>
    
    <div class="login-footer">
      <p>© 2024 中医科普平台 版权所有</p>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'
import { login, register } from '@/api/user'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const formRef = ref(null)
const isLogin = ref(true)
const rememberMe = ref(false)

const form = ref({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    loading.value = true
    
    if (isLogin.value) {
      // 登录
      const success = await userStore.login(form.value)
      if (success) {
        ElMessage.success('登录成功')
      }
    } else {
      // 注册
      const response = await register(form.value)
      if (response.code === '0000') {
        ElMessage.success('注册成功')
        isLogin.value = true // 切换到登录模式
      }
    }
  } catch (error) {
    console.error(isLogin.value ? '登录失败:' : '注册失败:', error)
    ElMessage.error(isLogin.value ? '登录失败' : '注册失败')
  } finally {
    loading.value = false
  }
}

const switchMode = () => {
  isLogin.value = !isLogin.value
  form.value = {
    username: '',
    password: ''
  }
  if (formRef.value) {
    formRef.value.clearValidate()
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: linear-gradient(135deg, #E3F2FD 0%, #BBDEFB 100%);
  
  .login-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 60px 20px;
    
    .login-header {
      text-align: center;
      margin-bottom: 30px;
      
      .logo {
        width: 64px;
        height: 64px;
        margin: 0 auto 16px;
        background: var(--el-color-primary);
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 32px;
        font-weight: bold;
        border-radius: 16px;
      }
      
      .title {
        font-size: 24px;
        color: #2c3e50;
        margin: 0 0 8px;
      }
      
      .subtitle {
        font-size: 16px;
        color: #606266;
        margin: 0;
      }
    }
    
    .login-form {
      width: 100%;
      max-width: 400px;
      padding: 40px;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 16px;
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
      
      :deep(.el-input__wrapper) {
        border-radius: 8px;
      }
      
      .login-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 24px;
      }
      
      .login-button {
        width: 100%;
        height: 44px;
        border-radius: 8px;
        font-size: 16px;
      }
      
      .register-link {
        text-align: center;
        margin-top: 16px;
        color: #606266;
      }
    }
  }
  
  .login-footer {
    text-align: center;
    padding: 16px;
    color: #606266;
    background: rgba(255, 255, 255, 0.9);
  }
}

// 响应式设计
@media screen and (max-width: 480px) {
  .login-container {
    .login-content {
      padding: 40px 16px;
      
      .login-header {
        .title {
          font-size: 28px;
        }
        
        .subtitle {
          font-size: 14px;
        }
      }
      
      .login-form {
        padding: 24px;
      }
    }
  }
}
</style> 